"use strict";

var timeout;
app.controller("OpeCenTimeController", ["$scope", "$modal", "$timeout", "RESTService",
    function ($scope, $modal, $timeout, RESTService) {
        $scope.page = {
            totalItems: 0,
            currentPage: 1,
            currentItems: 0,
            maxSize: 5,				//max size of pagination
            itemsPerPage: 10
        }
        $scope.setPage = function (pageNo) {
            $scope.page.currentPage = pageNo;
        };
        $scope.pageChanged = function () {
            $scope.loadOpeCenTimeListData();
        };
        $scope.paid = "false";
        $scope.like = "";

        $scope.opeCenTimes = [];
        // load Operation Center Time data
        var stateflag = 0;
        $scope.loadOpeCenTimeListData = function (state) {
            if (state != null) {
                stateflag = state;
            }
            var params = {
                hosp_Id: "",
                like: $scope.like,
                pageNo: $scope.page.currentPage,
                pageSize: $scope.page.itemsPerPage,
                flag: stateflag
            };

            RESTService.getResponse("/rest/opecentime/getFinishedOpeCenTimesCount?" + $.param(params)).then(function (response) {
                $scope.page.totalItems = response[0].count;
            });
            RESTService.getResponse("/rest/opecentime/getFinishedOpeCenTimes?" + $.param(params))
                .then(function (response) {
                    $scope.opeCenTimes = response;
                    $scope.page.currentItems = $scope.opeCenTimes.length;
                });
        }
        $scope.init = function () {
            $scope.loadOpeCenTimeListData();
        }

        $scope.getServReqByOCT = function (opeCenTime) {
            $scope.servReqs = opeCenTime._servReqs;
            var modalInstance = $modal.open({
                backdrop: 'static',
                templateUrl: 'ServReqsBYOCT.html',
                controller: 'ModalInstanceCtrl',
                size: 'lg', resolve: {
                    items: function () {
                        return $scope.servReqs;
                    }
                },
                scope: $scope
            });
            modalInstance.result.then(function () {
            }, function () {
            });
        }

        //3 open modal
        $scope.open = function (attrs, model) {
            // model.scope = $scope;
            var modalInstance = $modal.open(angular.extend({
                backdrop: 'static',
                controller: "ModalInstanceCtrl",
                resolve: {
                    items: function () {
                        return [Object(model)];
                    }
                },
                scope: $scope
            }, attrs));
        };

        // share case
        $scope.sharePatient = function (a, b) {
            var patient_Id = a.patient_Id ? a.patient_Id : a.patient_Id2;
            var params = {
                patient_Id: patient_Id
                , friendId: b
            }
            console.log(params);
            RESTService.postResponse('/rest/chat/createChatMessageSharingPatient?', params).then(
                function (suc) {
                    console.log(suc);
                });
        }
    }]);

